import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from "@/views/Home"
import Nav from '@/components/Nav'
import Footer from '@/components/Footer'
import NavCart from '@/components/NavCart'
import Cart from "@/views/Cart"
import Login from "@/views/Login"
import store from '../store'
import CartChild1 from "@/views/cart/CartChild1"
import CartChild2 from "@/views/cart/CartChild2"
import CartChild3 from "@/views/cart/CartChild3"
import AboutUs from "@/views/cart/AboutUs"
import Hezuo from "@/views/cart/Hezuo"
import ShoppingCar from "@/views/cart/ShoppingCar"
import GiftMall from "@/views/GiftMall"
import DetailsItem from "@/components/DetailsItem"

Vue.use(VueRouter)

const router = new VueRouter({


    routes: [
        {
            path: '/',
            components: {
                nav: Nav,
                default: Home,
                footer: Footer,
                navCart: NavCart
            }
        },
        {
            path: '/cart',
            components: {
                nav: Nav,
                default: Cart,
                footer: Footer,
            },
            children: [{
                path: '/',
                component: CartChild1
            },
            {
                path: 'child1',
                component: CartChild2
            },
            {
                path: 'child2',
                component: CartChild3
            },
            {
                path: 'shopcar',
                component: ShoppingCar
            },
            {
                path: 'child3',
                component: AboutUs
            },
            {
                path: 'child4',
                component: Hezuo
            },
            ]

        },
        {
            path: '/login',
            component: Login
        },
        {
            path: '/giftmall/:id',
            components: {
                nav: Nav,
                default: GiftMall,
                footer: Footer,
                navCart: NavCart
            }
        },
        {
            path: '/details/:id',
            components: {
                nav: Nav,
                default: DetailsItem,
                footer: Footer,
                navCart: NavCart
            }
        }
    ]
})

router.beforeEach((to, from, next) => {
    console.log(from)
    let { loginState } = store.state
    if (to.path != '/cart') {
        next()
    } else {
        if (loginState) {
            next()
        } else {
            next('/login')
        }
    }
})


router.beforeEach((to, from, next) => {
    console.log(from)
    let {
        loginState
    } = store.state
    if (to.path != '/cart') {
        next()
    } else {
        if (loginState) {
            next()
        } else {
            next('/login')
        }
    }
})

export default router


